import {
  sutravel
} from 'sutravel-model.js';
var api = new sutravel();

var Function = require("../../../../utils/function.js");
var Tool = require("../../../../utils/upload.js");
let App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //出差事由
    reason: false,
    //交通方式
    vehicle: false,
    vehicleList: [{
        id: 1,
        text: '飞机'
      },
      {
        id: 2,
        text: '火车'
      },
      {
        id: 3,
        text: '汽车'
      },
      {
        id: 4,
        text: '其他'
      }
    ],
    //单程往返
    trip: false,
    tripList: [{
        id: 1,
        text: '单程'
      },
      {
        id: 2,
        text: '往返'
      }
    ],
    //出发城市
    depart: false,
    //到达城市
    arrive: false,
    //开始时间
    startDate: false,
    startStamp: false,
    //结束时间
    endDate: false,
    endStamp: false,
    //具体时间
    starTime:false,
    endTime:false,
    //时长
    duration: false,
    //天数
    days: false,
    //备注
    remarks: false,
    //上传的图片
    listImg: []
  },
   //出差事由
   bindReason(e){
    this.setData({ reason: e.detail.value})
  },

  //选择交通方式
  bindVehicle(e) {
    this.setData({
      vehicle: e.detail.value
    })
  },

  //选择单程往返
  bindTrip(e) {
    this.setData({
      trip: e.detail.value
    })
  },

  //选择开始时间
  bindStart(e) {
    let timeStamp = new Date(e.detail.value).getTime();
    this.setData({
      startDate: e.detail.value,
      startStamp: timeStamp
    })
    if (this.data.startDate && this.data.endDate) {
      this.bindLength();
    }
  },

  //选择结束时间
  bindEnd(e) {
    let timeStamp = new Date(e.detail.value).getTime();
    this.setData({
      endDate: e.detail.value,
      endStamp: timeStamp
    })
    if (this.data.startDate && this.data.endDate) {
      this.bindLength();
    }
  },

  //选择开始具体时间
  bindStartime(e) {
    this.setData({ starTime: e.detail.value })
  },

  //选择结束具体时间
  bindEndtime(e) {
    this.setData({ endTime: e.detail.value })
  },

  //时长
  bindLength() {
    if (this.data.startStamp < this.data.endStamp) {

      let leng = this.data.endStamp - this.data.startStamp;
      let dateLeng = parseInt(leng / 1000 / 60 / 60);
      let dateDay = parseInt(leng / 1000 / 60 / 60 / 24);
      this.setData({
        duration: dateLeng,
        days: dateDay
      })

    } else if (this.data.startStamp == this.data.endStamp) {

      this.setData({
        duration: 24,
        days: 1
      })

    } else if (this.data.startStamp > this.data.endStamp) {

      let leng = this.data.startStamp - this.data.endStamp;
      let dateLeng = parseInt(leng / 1000 / 60 / 60);
      let dateDay = parseInt(leng / 1000 / 60 / 60 / 24);
      this.setData({
        duration: dateLeng,
        days: dateDay
      })

    }
  },

  //上传图片
  bindUpload() {
    Tool.upload().then(res => {
      let listImg = this.data.listImg;
      listImg.push(res);
      this.setData({
        listImg: listImg
      })
    });
  },

  //删除上传的图片
  bindDelete(e) {
    wx.showModal({
      content: '删除当前图片？',
      success: (res) => {
        if (res.confirm) {
          let listImg = this.data.listImg;
          listImg.splice(e.target.dataset.index, 1)
          this.setData({
            listImg: listImg
          })
        }
      }
    })
  },

  //备注
  bindRemarks(e) {
    this.setData({
      remarks: e.detail.value
    })
  },
  //获取审批人
  bindPeople(){
    api.getPeople({}, (res) => {
      if (res.code == 0) {
        App.showError(res.msg, function () {
          wx.navigateBack();
        });
      } else {
        let peopid = new Array();
        res.data.map(item => {
          peopid.push(item.id)
        })
        this.setData({
          people: res.data,
          peopid: peopid
        })
      }
    })
  },
  //提交出差
  bindOut() {
    if (!this.data.reason) {
      Function.layer('请输入出差事由')
    } else if (!this.data.vehicle) {
      Function.layer('请选交通方式')
    } else if (!this.data.trip) {
      Function.layer('请选单程往返')
    } else if (!this.data.depart) {
      Function.layer('请选出发城市')
    }else if (!this.data.arrive) {
      Function.layer('请选到达城市')
    } else if (!this.data.startDate) {
      Function.layer('请选开始日期')
    } else if (!this.data.endDate) {
      Function.layer('请选结束日期')
    } else if (this.data.startDate == this.data.endDate){
      Function.layer('不能选择相同时间段，如当天差事，结束时间选择后一天')
    } else if (!this.data.starTime){
      Function.layer('请选具体开始时间')
    } else if (!this.data.endTime){
      Function.layer('请选具体结束时间')
    }
    else {
      api.postEvection({
        approver: this.data.peopid,
        start_time: this.data.startDate + '  ' + this.data.starTime,
        end_time: this.data.endDate + '  ' + this.data.endTime,
        cause: this.data.reason,
        trip: this.data.trip,
        transport: this.data.vehicle,
        note: this.data.remarks,
        departure_city: this.data.depart,
        terminal_city: this.data.arrive,
        img:this.data.listImg,
        days: this.data.days,
        duration: this.data.duration,
      }, (res) => {
        if (res.code == 1) {
          App.showSuccess(res.msg, function () {
            wx.navigateBack();
          });

        } else {
          if(res.code == 1){
            App.showSuccess(res.msg, function () {
              wx.navigateBack();
            });
         
          }else{
         
            Function.layer(res.msg)
          }
        }
      })
    }
  },

  //路由跳转
  linkTo(e) {
    let url = Function.getDataSet(e, 'url');
    Function.linkTo(url)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    _this.setData({
      isLogin: App.checkIsLogin()
    });
    console.log(this.data.isLogin)
    if (_this.data.isLogin === false) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    this.bindPeople();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})